package com.leyou.user.controller;

import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.pojo.AddressDTO;
import com.leyou.user.pojo.User;
import com.leyou.user.pojo.UserDTO;
import com.leyou.user.pojo.UserDetail;
import com.leyou.user.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.stream.Collectors;

/**
 * 用户控制器
 */
@RestController
@Api(description = "用户模块")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 数据校验
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkData(@PathVariable("data") String data,@PathVariable("type") Integer type){
        Boolean isUse = userService.checkData(data,type);
        return ResponseEntity.ok(isUse);
    }

    /**
     * 发送手机验证码
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendSmsCode(@RequestParam("phone") String phone){
        userService.sendSmsCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 用户注册
     * BindingResult: 封装了所有字段验证失败的错误信息
     */
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user, BindingResult result, @RequestParam("code") String code){
        if(result.hasErrors()){
            //取出错误信息
            String errorMsg = result.getFieldErrors()
                                        .stream()
                                        .map(FieldError::getDefaultMessage)
                                        .collect(Collectors.joining("|"));

            //自行封装异常信息
            throw new LyException(500,errorMsg);
        }

        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 判断用户名和密码是否正确
     */
    @GetMapping("/query")
    public ResponseEntity<User> findUserByNameAndPwd(@RequestParam("username") String username,@RequestParam("password") String password){
        User loginUser = userService.findUserByNameAndPwd(username,password);
        return ResponseEntity.ok(loginUser);
    }

    /**
     * 查询指定用户的指定收货地址
     */
    @GetMapping("/address")
    public ResponseEntity<AddressDTO> findAddressById(@RequestParam("userId") Long userId,@RequestParam("id") Long id){
        AddressDTO addressDTO = userService.findAddressById(userId,id);
        return ResponseEntity.ok(addressDTO);
    }

    /**
     * 获取左侧列表的用户(详情)信息
     */
    @GetMapping("/user")
    public ResponseEntity<UserDTO> findUser(HttpServletRequest request){
        UserDTO user = userService.findUser(request);
        return ResponseEntity.ok(user);
    }

    /**
     * 获取用户详情信息
     */
    @GetMapping("/search")
    public ResponseEntity<UserDetail> findUserDetail(HttpServletRequest request){
        UserDetail userDetail = userService.findUserDetail(request);
        return ResponseEntity.ok(userDetail);
    }

    /**
     * 获取表单数据
     */
    @PostMapping("/save")
    public ResponseEntity<Void> saveUser(UserDetail userDetail,HttpServletRequest request){
        userService.saveUser(userDetail,request);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 保存图片
     */
    @PutMapping
    public ResponseEntity<Void> saveImg(@RequestParam("image") String image,HttpServletRequest request){
        userService.saveImg(image,request);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
